<?php


namespace app\model;


use think\facade\Db;
use think\response\Json;

class CommentModel
{
    private static $_baseTable = 'eb_order'; // 订单表
    private static $_storeTable = 'eb_store'; // 商户表

    /** 获取评论列表
     * @param $input
     * @param $token
     * @return array|\think\Model|Json|null
     */
    public static function list($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            // 获取列表
            $table = Db::table(self::$_baseTable)->alias('o')->join([self::$_storeTable => 's'], 's.id = o.store_id');
            $table->field('o.id, o.order_no, o.comment, o.is_show,s.account,s.shop_name');
            $map = [];
            $map[] = ['operate_id', '=', $rtn['id']];
            if ($input['is_show'] !== '') $map[] = ['is_show', '=', $input['is_show']];
            if ($input['store_id'] !== '') $map[] = ['o.store_id', '=', $input['store_id']];
            $table->where($map);
            $count = $table->count();
            if ($count == 0 ) return jsonSuccess('查询成功', ['count'=> $count, 'list' => []]);
            $list = $table->order('o.create_time', 'desc')->page($input['page'], $input['limit'])->select();
            return jsonSuccess('查询成功', ['count'=> $count, 'list' => $list]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**修改是否隐藏
     * @param $input
     * @param $token
     * @return array|\think\Model|Json|null
     */
    public static function changeShow($input, $token)
    {
        try {
            // 检测当前管理员状态
            $rtn = AdminModel::getInfo($token, [1], [3]);
            if ($rtn instanceof Json) return $rtn;
            $rtn = Db::table(self::$_baseTable)->update($input);
            if ($rtn === 1) return  jsonSuccess('修改成功');
            return  jsonFail('修改失败');
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
}